
<template>
  <div class="baseuser">
    <div class="btns">
      <!-- 检索框 -->
      <el-form :inline="true" :model="params" size="small">
        <el-button
          @click="toAddHandler"
          type="primary"
          size="small"
          style="margin-right: 10px"
          >添加</el-button
        >
        <el-form-item>
          <el-input v-model="params.username" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select clearable v-model="params.status" placeholder="请选择状态">
            <el-option label="正常" value="正常"></el-option>
            <el-option label="禁用" value="禁用"></el-option>
          </el-select>
        </el-form-item>
        <el-button
          type="primary"
          size="small"
          style="margin-right: 10px"
          @click="pageQueryBaseUser"
          icon="el-icon-search"
          >查询</el-button
        >
      </el-form>
    </div>
    <div class="content">
      <!-- 表格 -->
      <el-table :data="resData.list" style="width: 100%" size="small">
        <el-table-column type="index" label="顾客编号"></el-table-column>
        <el-table-column label="用户名" prop="username"></el-table-column>
        <el-table-column label="姓名" prop="realname"></el-table-column>
        <el-table-column label="手机号" prop="telephone"></el-table-column>
        <el-table-column label="性别" prop="gender">
          

        </el-table-column>
        <el-table-column label="状态" prop="status">
          <template v-slot="scope">
            <el-tag
              v-if="scope.row.status == '正常'"
              size="mini"
              type="success"
              >{{ scope.row.status }}</el-tag>
            <el-tag v-else size="mini" type="danger">{{
              scope.row.status
            }}</el-tag>
          </template>
        </el-table-column>

        
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" 
            @click="editHandler(scope.row)">编辑</el-button>
            <el-button
              type="text"
              style="color: red"
              @click="DeleteHandler(scope.row.id, $event)">删除</el-button>
            <el-button 
              @click="toDetailHandler(scope.row.id)" 
              type="text">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="prev, pager, next, sizes"
        :total="resData.total"
        :page-size="params.pageSize"
        :current-page="params.page"
        @current-change="pageChangeHandler"
        :page-sizes="[5, 10, 15]"
        @size-change="pageSizeChangeHandler"
      >
      </el-pagination>
      <!-- 模态框 -->
      <el-dialog
        :title="title"
        :visible.sync="visible"
        @close="dialogCloseHandler"
      >
        <el-form :model="baseuser" :rules="rules" ref="saveForm">
          <el-form-item
            label="用户名"
            :label-width="formLabelWidth"
            prop="username"
          >
            <el-input v-model="baseuser.username"></el-input>
          </el-form-item>
          <el-form-item
            label="姓名"
            :label-width="formLabelWidth"
            prop="realname"
          >
            <el-input v-model="baseuser.realname"></el-input>
          </el-form-item>
          <el-form-item
            label="手机号"
            :label-width="formLabelWidth"
            prop="telephone"
          >
            <el-input v-model="baseuser.telephone"></el-input>
          </el-form-item>
          <el-form-item
            label="密码"
            :label-width="formLabelWidth"
            prop="password"
          >
            <el-input type="password" v-model="baseuser.password"></el-input>
          </el-form-item>
          <el-form-item
            label="性别"
            :label-width="formLabelWidth"
            prop="gender"
          >
           <el-radio v-model="baseuser.gender" label="male">男</el-radio>
          <el-radio v-model="baseuser.gender" label="female">女</el-radio>
            <!-- <el-input v-model="baseuser.gender"></el-input> -->
          </el-form-item>
           <el-form-item
            label="状态"
            :label-width="formLabelWidth"
            prop="status"
          >
           <el-radio v-model="baseuser.status" label="正常">正常</el-radio>
          <el-radio v-model="baseuser.status" label="禁用">禁用</el-radio>
            <!-- <el-input v-model="baseuser.gender"></el-input> -->
          </el-form-item>
          
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSavehandler">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import requset from "@/utils/request";
import { mapActions } from "vuex";
import qs from "qs";
import _ from "lodash";
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 5,
      },
      resData: {},
      categories: [],
      title: "",
      visible: false,
      baseuser: {},
      formLabelWidth: "80px",
      imageUrl: null,
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        realname: [
          {required: true,message: "姓名不能为空",trigger: "blur",},
        ],
        telephone: [
          { required: true, message: "电话号码不能为空", trigger: "blur" },
        ],
        gender: [
            { required: true, message: "性别不能为空", trigger: "blur" },
            ],
        password:[
            { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        status:[
            { required: true, message: "状态不能为空", trigger: "blur" }
        ],
      },
    };
  },
  methods: {
    // 导入状态机中的方法
    ...mapActions("category", ["findAllCategories"]),
    // 分页查询产品表格
    async pageQueryBaseUser() {
      // 过滤数据将空字符串删除
      for (let key in this.params) {
        if (this.params[key] == "") {
          delete this.params[key];
        }
      }
      let res = await requset.get("/baseUser/pageQuery", {
        params: this.params,
      });
      this.resData = res.data;
    },
    // page改变事件
    pageChangeHandler(page) {
      this.params.page = page;
      this.pageQueryBaseUser();
    },
    // pageSize改变事件
    pageSizeChangeHandler(pageSize) {
      this.params.pageSize = pageSize;
      this.pageQueryBaseUser();
    },
    // 新增按钮点击事件
    toAddHandler() {
      this.baseuser = {};
      this.imageUrl = null;
      this.title = "新增顾客";
      this.visible = true;
     
    },
    // 图片上传成功的回调
    handlePhotoSuccess(res, file) {
      // 将返回的图片在线地址映射到数据模型中
      this.production.photo = res;
      this.imageUrl = res;
    },
    // 表单保存事件
    toSavehandler() {
      this.$refs["saveForm"].validate((valid) => {
        if (valid) {
          requset
            .post("/baseUser/saveOrUpdate", qs.stringify(this.baseuser))
            .then((res) => {
              this.pageQueryBaseUser(); // 重载数据
              this.$message.success(res.message); // 提示操作结果信息
              this.visible = false; // 关闭模态框
            });
        } else {
          return false;
        }
      });
    },
    // 详情按钮点击事件
    toDetailHandler(id) {
      this.$router.push({
        path: "detail",
        query: { id },
      });
    },
    // 模态框关闭的回调
    dialogCloseHandler() {
      this.$refs["saveForm"].resetFields(); // 置空表单验证
    },
    // 编辑按钮点击事件
    editHandler(row) {
      this.baseuser = _.clone(row); // 将当前行数据赋值给数据模型
      this.title = "编辑产品信息";
      this.visible = true;
    },
    // 删除执行处理程序
    DeleteHandler(id, e) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
          requset.get("/baseUser/deleteById", { params: { id } }).then((res) => {
            this.pageQueryBaseUser(); // 重载数据
            this.$message.success(res.message); // 提示信息
          });    
      });
    },
  },
  created() {
    this.pageQueryBaseUser(); // 分页查询产品
    this.findAllCategories().then((res) => {
      this.categories = res.data.list;
    }); // 查询所有产品栏目
  },
  
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
